module RailsEnhancer
  class IncludeLineInjector

    def initialize(controller_file_path, controller_file_name)
      @regex = /(^class(\s*)GeneratorTestsController(\s*)<(\s*)(.*)$)|(^class(\s*)GeneratorTestsController(\s*)$)/
      @controller_file_path = controller_file_path
      @controller_file_name = controller_file_name
    end

    def run
      tmp = Tempfile.open("#{@controller_file_name}.rb") do |fp|
        File.foreach(@controller_file_path) do |line|
          fp.puts line
          if @regex.match(line)
            fp.puts 'include RailsEnhancer::ControllerEnhancer'
          end
        end
        fp
      end
      FileUtils.copy(tmp.path, @controller_file_path)
      tmp.unlink
    end
  end
end